package org.dong.Pojo;

import com.alibaba.fastjson.JSON;
import org.dong.Tool.Data;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class HttpResponse {
    private String ResponsePtl = "HTTP/1.1";
    private String ResponseStatus = "200";
    private String ResponseMsg = "OK";
    private String mimeType = "text/plain";
    private int length;
    private final String BR = "\r\n";


    public HttpResponse(OutputStream os,String url,String msg) {

        StringBuilder sb = new StringBuilder();
        //判断是静态资源还是动态资源
        if(url.contains(".")){
            //获取用户请求的资源路径
            File file;
            file = new File("static" + url);
            if (!file.exists()) {
                this.ResponseStatus = "404";
                this.ResponseMsg = "Not Found";
            }else{
                //获取mimeType
                String path = file.getPath();
                this.mimeType = Data.getMIMEType(path);
            }



            if(ResponseStatus.equals("200")){

                sb.append(ResponsePtl+" "+ResponseStatus + " "+ResponseMsg +""+BR);
                sb.append("Content-type:" + mimeType +""+BR);
                sb.append("Content-Length:" + file.length() +""+BR);
                sb.append(BR);

                try {
                    os.write(sb.toString().getBytes(StandardCharsets.UTF_8));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else if(ResponseStatus.equals("404")){

                String notFoundMessage = "<html> <meta charset=\"utf-8\"><body><h1>"+msg+"</h1></body></html>";
                sb.append(ResponsePtl+" "+ResponseStatus + " "+ResponseMsg+""+BR);
                sb.append("Content-type:"+mimeType+""+BR);
                sb.append("Content-Length:" + notFoundMessage.length() + ""+BR);
                sb.append(BR);
                try {
                    os.write(sb.toString().getBytes(StandardCharsets.UTF_8));
                    os.write(notFoundMessage.getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


            try {

                //创建字节组
                byte[] bs = new byte[0];
                if(file.exists()){

                    //创建输出流
                    FileInputStream fis = new FileInputStream(file);
                    bs = new byte[fis.available()];
                    fis.read(bs);
                }

                os.write(bs);
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }



        }else{

            sb.append(ResponsePtl+" "+ResponseStatus + " "+ResponseMsg +""+BR);
            sb.append("Content-type:"+mimeType+""+BR);
            sb.append("Content-Length:" + msg.getBytes().length + ""+BR);
            sb.append(BR);
            try {
                os.write(sb.toString().getBytes(StandardCharsets.UTF_8));
                os.write(msg.getBytes(StandardCharsets.UTF_8));
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }



    }
}




